package org.shanktech.tshare.binlogsync.controller;

import org.shanktech.tshare.binlogsync.model.api.StartRequestParam;
import org.shanktech.tshare.binlogsync.service.ManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value="manage", description="管理接口", produces=MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping(value="/manage")
public class ManagerController {
  
  private static final Logger LOGGER = LoggerFactory.getLogger(ManagerController.class);
  
  @Autowired
  private ManagerService managerService;

  @RequestMapping(value = "/start", method = {RequestMethod.GET,
      RequestMethod.POST})
  @ApiOperation(value = "启动转换", httpMethod = "POST && GET", response = String.class, notes = "启动转换")
  public String startConvert(
      @ApiParam(required=false, name="param", value="启动转换请求参数")
      @RequestBody(required = false) StartRequestParam param) {
    try {
      managerService.startOpenReplicator(param);
    } catch (Exception e) {
      LOGGER.error("url {manage/start},  catch a exception!", e);
      return "error";
    }
    return "success";
    
  }

  @RequestMapping(value = "/stop", method = {RequestMethod.GET})
  @ApiOperation(value = "停止转换", httpMethod = "GET", response = String.class, notes = "停止转换")
  public String stopConvert() {
    try {
      managerService.stopOpenReplicator();
    } catch (Exception e) {
      LOGGER.error("url {manage/stop},  catch a exception!", e);
      return "error";
    }
    return "success";
  }
}
